%% remove solar tidal signals


%define solar and lunar periods
periods = [4.0000    4.8000    6.0000    8.0000   11.9672   12.0000   12.4210   12.6583   23.9345   24.0000 ];
data_array = E_phi(:,1);
%create a model
m=[cos(2*pi*t(:)/(periods(1)/24) ) sin(2*pi*t(:)/(periods(1)/24) )];

for j=2:length(periods)
    m = [m cos(2*pi*t(:)/(periods(j)/24) ) sin(2*pi*t(:)/(periods(j)/24) )];
end


% LS fitting
a = m\data_array;

P = angle(complex(a(2:2:end),a(1:2:end))); % 
A = abs(complex(a(2:2:end),a(1:2:end)));  % BUG found. I had used arctan (x/y) 

% create the synthetic time series

if exist('y'),
clear y
end;

for i = 1:length(P),
    y(i,:) = A(i) * sin(2*pi*t*24/periods(i) + P(i)) ;
end;


data_corrected = data_array - sum(y); % do this for all the components
% and replace the data vector  with corrected data.
